package com.yalu.util;

import com.yalu.entity.BillDetailData;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.regex.Pattern;

public class CommonUtil {

    public static final String AMOUNT_MATCHES = "[+-]*(([1-9]{1}\\d*)|(0{1}))[.][0-9]{2}";

    public static final Pattern PATTERN = Pattern.compile(AMOUNT_MATCHES);

    public static final String AMOUNT_MATCHES_T = "([+-]*(([1-9]{1}\\d*)|(0{1}))[.][0-9]{2})|([.][0-9]{2})";

    public static final Pattern PATTERN_T = Pattern.compile(AMOUNT_MATCHES_T);

    public static final String AMOUNT_MATCHES_C = "([A-Z]{3} [+-.]*\\d*)|([A-Z]{3} [A-Z]{3} [+-.]*\\d*)";

    public static final Pattern PATTERN_C = Pattern.compile(AMOUNT_MATCHES_C);

    public static final String AMOUNT_MATCHES_CC = "[A-Z]{3}";

    public static final Pattern PATTERN_CC = Pattern.compile(AMOUNT_MATCHES_CC);

    /**
     * 添加存款
     *
     * @param one
     * @param detailData
     * @param two
     */
    public static void addDeposit(String one, BillDetailData detailData, String two) {
        String lastBalance = detailData.getBalance();
        if (StringUtils.hasText(one) && StringUtils.hasText(two) && one.matches(AMOUNT_MATCHES) && two.matches(AMOUNT_MATCHES)) {
            detailData.setBalance(one);
            detailData.setDeposit(two);
            detailData.setWithdrawal(null);
            if (StringUtils.hasText(lastBalance)) {
                BigDecimal now = new BigDecimal(one);
                BigDecimal last = new BigDecimal(lastBalance);
                BigDecimal result = now.subtract(last);
                BigDecimal zero = new BigDecimal(0);
                if (zero.compareTo(result) == 1) {
                    detailData.setBalance(one);
                    detailData.setDeposit(null);
                    detailData.setWithdrawal(two);
                }
            }
        } else {
            if (StringUtils.hasText(one) && one.matches(AMOUNT_MATCHES)) {
                detailData.setDeposit(one);
                if (StringUtils.hasText(lastBalance)) {
                    BigDecimal now = new BigDecimal(one);
                    BigDecimal last = new BigDecimal(lastBalance);
                    BigDecimal result = last.add(now);
                    detailData.setBalance(result.toString());
                }
            } else {
                detailData.setDeposit(null);
                detailData.setBalance(null);
            }
            detailData.setWithdrawal(null);
        }
    }

    /**
     * 添加提款
     *
     * @param one
     * @param detailData
     * @param two
     */
    public static void addWithdrawal(String one, BillDetailData detailData, String two) {
        String lastBalance = detailData.getBalance();
        if (StringUtils.hasText(one) && StringUtils.hasText(two) && one.matches(AMOUNT_MATCHES) && two.matches(AMOUNT_MATCHES)) {
            detailData.setBalance(one);
            detailData.setWithdrawal(two);
            detailData.setDeposit(null);
            if (StringUtils.hasText(lastBalance)) {
                BigDecimal now = new BigDecimal(one);
                BigDecimal last = new BigDecimal(lastBalance);
                BigDecimal result = now.subtract(last);
                BigDecimal zero = new BigDecimal(0);
                if (zero.compareTo(result) == -1) {
                    detailData.setBalance(one);
                    detailData.setDeposit(two);
                    detailData.setWithdrawal(null);
                }
            }
        } else {
            if (StringUtils.hasText(one) && one.matches(AMOUNT_MATCHES)) {
                detailData.setWithdrawal(one);
                if (StringUtils.hasText(lastBalance)) {
                    BigDecimal now = new BigDecimal(one);
                    BigDecimal last = new BigDecimal(lastBalance);
                    BigDecimal result = last.subtract(now);
                    detailData.setBalance(result.toString());
                }
            } else {
                detailData.setWithdrawal(null);
                detailData.setBalance(null);
            }
            detailData.setDeposit(null);
        }
    }

    /**
     * 添加存款
     *
     * @param one
     * @param detailData
     * @param two
     */
    public static String analyzeTransactionType(String one, BillDetailData detailData, String two) {
        String lastBalance = detailData.getBalance();
        if (StringUtils.hasText(lastBalance) && StringUtils.hasText(one) && StringUtils.hasText(two) && one.matches(AMOUNT_MATCHES) && two.matches(AMOUNT_MATCHES)) {
            BigDecimal now = new BigDecimal(one);
            BigDecimal last = new BigDecimal(lastBalance);
            BigDecimal result = now.subtract(last);
            BigDecimal zero = new BigDecimal(0);
            if (zero.compareTo(result) == 1) {
                detailData.setBalance(one);
                detailData.setDeposit(null);
                detailData.setWithdrawal(two);
                return "WITHDRAWAL";
            } else {
                detailData.setBalance(one);
                detailData.setDeposit(two);
                detailData.setWithdrawal(null);
                return "DEPOSIT";
            }
        }
        return null;
    }
}
